home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue62 / WStrings / widequery.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-08-15  |  2.6 KB  |  99 lines

  1. unit WideQuery;
  2.  
  3. interface
  4.  
  5. // The InputQueryW function works like InputQuery, except it allows the
  6. // user to enter a wide string.
  7. // Copyright ⌐ 2000 Tempest Software, Inc.
  8.  
  9. uses
  10.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  11.   StdCtrls;
  12.  
  13. type
  14.   TInputQueryWForm = class(TForm)
  15.     Label1: TLabel;
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     procedure FormShow(Sender: TObject);
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure FormDestroy(Sender: TObject);
  21.   private
  22.     { Private declarations }
  23.     fEditWnd: HWND;
  24.     function GetText: WideString;
  25.     procedure SetText(const Value: WideString);
  26.   public
  27.     { Public declarations }
  28.     property EditWnd: HWND read fEditWnd;
  29.     property Text: WideString read GetText write SetText;
  30.   end;
  31.  
  32. function InputQueryW(const Caption, Prompt: string; var Value: WideString): Boolean;
  33.  
  34. implementation
  35.  
  36. {$R *.DFM}
  37.  
  38. function InputQueryW(const Caption, Prompt: string; var Value: WideString): Boolean;
  39. var
  40.   Form: TInputQueryWForm;
  41. begin
  42.   Form := TInputQueryWForm.Create(Application);
  43.   try
  44.     Form.Caption := Caption;
  45.     Form.Label1.Caption := Prompt;
  46.     Form.Text := Value;
  47.  
  48.     // Show the form modally so the user can edit the text.
  49.     Result := Form.ShowModal = mrOK;
  50.     if Result then
  51.       Value := Form.Text;
  52.   finally
  53.     Form.Free;
  54.   end;
  55. end;
  56.  
  57. { TInputQueryWForm }
  58.  
  59. procedure TInputQueryWForm.FormCreate(Sender: TObject);
  60. const
  61.   Margin = 8;
  62.   EditWidth = 300;
  63. begin
  64.   // Create the wide edit control.
  65.   fEditWnd := CreateWindowExW(Ws_Ex_ClientEdge, 'EDIT', '',
  66.                  Ws_Child or Ws_Visible or Ws_Border or Ws_TabStop or Es_AutoHScroll,
  67.                  Margin, Label1.BoundsRect.Bottom+Margin, EditWidth, Label1.Height + Margin,
  68.                  Handle, 0, hInstance, nil);
  69.  
  70.   // The default font uses the OEM character set, not Unicode.
  71.   // The form uses Arial, which supports Unicode on NT.
  72.   SendMessageW(EditWnd, Wm_SetFont, WParam(Font.Handle), 0);
  73. end;
  74.  
  75. procedure TInputQueryWForm.FormDestroy(Sender: TObject);
  76. begin
  77.   CloseWindow(EditWnd);
  78. end;
  79.  
  80. // When the form opens, set the focus to the edit control.
  81. procedure TInputQueryWForm.FormShow(Sender: TObject);
  82. begin
  83.   Windows.SetFocus(EditWnd);
  84. end;
  85.  
  86. function TInputQueryWForm.GetText: WideString;
  87. begin
  88.   // Get the text and return it to the caller.
  89.   SetLength(Result, GetWindowTextLengthW(EditWnd));
  90.   GetWindowTextW(EditWnd, PWideChar(Result), Length(Result)+1);
  91. end;
  92.  
  93. procedure TInputQueryWForm.SetText(const Value: WideString);
  94. begin
  95.   SetWindowTextW(EditWnd, PWideChar(Value));
  96. end;
  97.  
  98. end.
  99.